package com.lili.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lili.controller.request.XasUserRequest;
import com.lili.mapper.XasUserMapper;
import com.lili.po.XasUser;
import com.lili.po.XasRole;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {
    @Autowired
    XasUserMapper userMapper;

    public XasUser findUserByLoginName(String loginName) {
        LambdaQueryWrapper<XasUser> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(XasUser::getLoginName, loginName);
        return userMapper.selectOne(lambdaQueryWrapper);

    }

    public XasUser findUserByUserId(Integer uid) {
        return userMapper.selectUserById();
    }

    public IPage<XasUser> selectUser(XasUserRequest xasUserRequest) {
        Page page = new Page();
        BeanUtils.copyProperties(xasUserRequest, page);
        IPage<XasUser> iPage = userMapper.selectUser(page, xasUserRequest.getLoginName());
        return iPage;
    }

    @Transactional
    public void addXasUser(XasUser xasUser) {
        userMapper.insert(xasUser);
    }

    @Transactional
    public void updateXasUser(XasUser xasUser) {
        userMapper.updateById(xasUser);
    }

    @Transactional
    public void delXasUser(Integer uid) {
        userMapper.deleteById(uid);
    }


    public Map<String, String> selectAllUser() {
        Map<String, String> map = new HashMap<>();
        List<XasUser> xasUsers = userMapper.selectList(null);
        xasUsers.forEach(e -> {
            map.put(e.getUid().toString(), e.getLoginName());
        });
        return map;
    }

}
